package com.firmboy.springboot_axis2.config;

import org.apache.axiom.om.OMElement;
import org.apache.axis2.AxisFault;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.deployment.DescriptionBuilder;
import org.apache.axis2.deployment.ServiceBuilder;
import org.apache.axis2.deployment.ServiceGroupBuilder;
import org.apache.axis2.deployment.repository.util.ArchiveReader;
import org.apache.axis2.deployment.repository.util.DeploymentFileData;
import org.apache.axis2.description.AxisService;
import org.apache.axis2.description.AxisServiceGroup;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.xml.stream.XMLStreamException;
import java.util.ArrayList;
import java.util.Map;

/**
 * @author firmboy
 * @create 2018-06-08 下午8:40
 **/
public class ArchiveReaderExport extends ArchiveReader {


    private static final Log log = LogFactory.getLog(ArchiveReader.class);


    @Override
    public ArrayList<AxisService> buildServiceGroup(OMElement serviceMetaData,
                                                    DeploymentFileData currentFile, AxisServiceGroup axisServiceGroup,
                                                    Map<String, AxisService> wsdlServices, ConfigurationContext configCtx)
            throws XMLStreamException, AxisFault {

        String elementName = serviceMetaData.getLocalName();

        if (TAG_SERVICE.equals(elementName)) {
            AxisService axisService = null;
            String serviceName = DescriptionBuilder.getShortFileName("test");
            if (serviceName != null) {
                axisService = wsdlServices.get(serviceName);
            }
            if (axisService == null) {
                axisService = wsdlServices.get(
                        DescriptionBuilder.getShortFileName("test"));
            }
            if (axisService == null) {
                axisService = new AxisService(serviceName);
            } else {
                axisService.setWsdlFound(true);
                axisService.setCustomWsdl(true);
            }

            axisService.setParent(axisServiceGroup);
            axisService.setClassLoader(currentFile.getClassLoader());

            ServiceBuilder serviceBuilder = new ServiceBuilder(configCtx, axisService);
            serviceBuilder.setWsdlServiceMap(wsdlServices);
            AxisService service = serviceBuilder.populateService(serviceMetaData);

            ArrayList<AxisService> serviceList = new ArrayList<AxisService>();
            serviceList.add(service);
            return serviceList;
        } else if (TAG_SERVICE_GROUP.equals(elementName)) {
            ServiceGroupBuilder groupBuilder = new ServiceGroupBuilder(serviceMetaData, wsdlServices,
                    configCtx);
            return groupBuilder.populateServiceGroup(axisServiceGroup);
        }
        throw new AxisFault("Invalid services.xml found");
    }

//
//    @Override
//    public void processFilesInFolder(File folder, HashMap<String, AxisService> servicesMap)
//            throws FileNotFoundException, XMLStreamException, DeploymentException {
//        File files[] = folder.listFiles();
//        for (int i = 0; i < files.length; i++) {
//            File file1 = files[i];
//            if (file1.getName().toLowerCase().endsWith(SUFFIX_WSDL)) {
//                InputStream in = new FileInputStream(file1);
//                FileInputStream in2;
//
//                // now the question is which version of WSDL file this archive contains.
//                // lets check the namespace of the root element and decide. But since we are
//                // using axiom (dude, you are becoming handy here :)), we will not build the
//                // whole thing.
//                OMNamespace documentElementNS = ((OMElement) XMLUtils.toOM(in)).getNamespace();
//                if (documentElementNS != null) {
//                    WSDLToAxisServiceBuilder wsdlToAxisServiceBuilder;
//                    if (WSDL2Constants.WSDL_NAMESPACE
//                            .equals(documentElementNS.getNamespaceURI())) {
//                        // we have a WSDL 2.0 document here.
//                        in2 = new FileInputStream(file1);
//                        wsdlToAxisServiceBuilder = new WSDL20ToAllAxisServicesBuilder(in2);
//                    } else if (Constants.NS_URI_WSDL11.
//                            equals(documentElementNS.getNamespaceURI())) {
//                        in2 = new FileInputStream(file1);
//                        wsdlToAxisServiceBuilder = new WSDL11ToAllAxisServicesBuilder(in2);
//                        ((WSDL11ToAxisServiceBuilder) wsdlToAxisServiceBuilder).setDocumentBaseUri(file1.toURI()
//                                .toString());
//                    } else {
//                        throw new DeploymentException(Messages.getMessage("invalidWSDLFound"));
//                    }
//
//                    FileInputStream in3 = new FileInputStream(file1);
//                    List<AxisService> services = processWSDLFile(wsdlToAxisServiceBuilder, file1, false,
//                            in2, file1.toURI().toString());
//
//                    if (services != null) {
//                        for (AxisService axisService : services) {
//                            if (axisService != null) {
//                                servicesMap.put(axisService.getName(), axisService);
//                            }
//                        }
//                    }
//                    try {
//                        in2.close();
//                        in3.close();
//                    } catch (IOException e) {
//                        log.info(e);
//                    }
//                }
//                try {
//                    in.close();
//                } catch (IOException e) {
//                    log.info(e);
//                }
//            }
//        }
//    }
//
//
//    /**
//     * Creats AxisService.
//     *
//     * @param in
//     * @return Returns AxisService.
//     * @throws DeploymentException
//     */
//    private List<AxisService> processWSDLFile(WSDLToAxisServiceBuilder axisServiceBuilder,
//                                              File serviceArchiveFile,
//                                              boolean isArchive, InputStream in, String baseURI)
//            throws DeploymentException {
//        try {
//
//            if (serviceArchiveFile != null && isArchive) {
//                axisServiceBuilder.setCustomResolver(
//                        new AARFileBasedURIResolver(serviceArchiveFile));
//                if (axisServiceBuilder instanceof WSDL11ToAllAxisServicesBuilder) {
//
//                    ((WSDL11ToAllAxisServicesBuilder) axisServiceBuilder).setCustomWSDLResolver(
//                            new AARBasedWSDLLocator(baseURI, serviceArchiveFile, in));
//
//                    ((WSDL11ToAllAxisServicesBuilder) axisServiceBuilder).setDocumentBaseUri(
//                            serviceArchiveFile.getCanonicalFile().toURI().toString());
//
//                } else if (axisServiceBuilder instanceof WSDL20ToAllAxisServicesBuilder) {
//                    // trying to use the jar scheme as the base URI. I think this can be used to handle
//                    // wsdl 1.1 as well without using a custom URI resolver. Need to look at it later.
//                    axisServiceBuilder.setBaseUri(
//                            "jar:" + serviceArchiveFile.toURI() + "!/" + baseURI);
//                }
//            } else {
//                if (serviceArchiveFile != null) {
//                    axisServiceBuilder.setBaseUri(
//                            serviceArchiveFile.getParentFile().toURI().toString());
//
//                    if (axisServiceBuilder instanceof WSDL11ToAllAxisServicesBuilder) {
//                        ((WSDL11ToAllAxisServicesBuilder) axisServiceBuilder).setDocumentBaseUri(
//                                serviceArchiveFile.getCanonicalFile().toURI().toString());
//                    }
//                }
//            }
//            if (axisServiceBuilder instanceof WSDL11ToAllAxisServicesBuilder) {
//                return ((WSDL11ToAllAxisServicesBuilder) axisServiceBuilder).populateAllServices();
//            } else if (axisServiceBuilder instanceof WSDL20ToAllAxisServicesBuilder) {
//                return ((WSDL20ToAllAxisServicesBuilder) axisServiceBuilder).populateAllServices();
//            }
//        } catch (AxisFault axisFault) {
//            log.info("Trouble processing wsdl file :" + axisFault.getMessage());
//            if (log.isDebugEnabled()) {
//                log.debug(axisFault);
//            }
//        } catch (IOException ioex) {
//            log.info("Trouble processing wsdl file :" + ioex.getMessage());
//            if (log.isDebugEnabled()) {
//                log.debug(ioex);
//            }
//        }
//        return null;
//    }
}
